import axios from 'axios'
import qs from 'qs'

axios.defaults.baseURL = ''

/**
 * application/json
 * application/x-www-form-urlencoded;charset=utf-8
 */
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'

axios.interceptors.request.use(

    config => {

        // 以 Form 形式请求
        let contentType = axios.defaults.headers['Content-Type']
        if(contentType && contentType.indexOf('www-form') > -1){
            config.transformRequest = params => {
                return qs.stringify(params)
            }
        }

        config.withCredentials = false
        config.responseType = 'json'
        return config
    },

    error => {
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(

    response => {
        if (response.status === 200) {
            return response.data
        }
        return response
    },
    error => {
        return Promise.reject(error)
    }
)

export default axios